<template>
    <div class="goods">
        <div class="sear">
            <el-input placeholder="请输入内容" v-model.trim="query" style="margin: 10px 10px 10px 0;width: 300px;"
                class="input-with-select">
                <el-button slot="append" icon="el-icon-search" @click="searchgoodlist"></el-button>
            </el-input>
            <el-button type="primary" @click="goAddGoods">添加商品</el-button>
        </div>
        <el-table :data="tableData" style="width: 100%">
            <el-table-column type="index" label="#">
            </el-table-column>
            <el-table-column prop="goods_name" label="商品名称">
            </el-table-column>
            <el-table-column prop="goods_price" label="价格(元)">
            </el-table-column>
            <el-table-column prop="goods_weight" label="商品重量">
            </el-table-column>
            <el-table-column label="创建时间">
                <template slot-scope="scope">
                    {{ scope.row.add_time | filterTime }}
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button type="primary" size="mini" icon="el-icon-edit" @click="handeledit(scope.row)"></el-button>
                    <el-button type="danger" size="mini" icon="el-icon-delete" @click="handeldelete(scope.row)"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pagenum"
            :page-sizes="[5, 10, 15, 20]" :page-size="pagesize" layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
    </div>
</template>
<script>
import { getgoodslist, deletegoodlist } from '../../utlis/api/shops/shopapi'
export default {
    data() {
        return {
            tableData: [],
            query: "",
            pagenum: 1,
            pagesize: 5,
            total: 0
        }
    },
    methods: {
        // 获取商品列表
        getgoodlist() {
            let obj = {
                query: this.query,
                pagenum: this.pagenum,
                pagesize: this.pagesize
            }
            getgoodslist(obj).then((res) => {
                console.log(43, res);
                this.tableData = res.data.data.goods
                this.total = res.data.data.total
            })
        },
        // 编辑
        handeledit(row) {
            console.log(58, row);
        },
        // 删除
        handeldelete(row) {
            console.log(62, row);
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
                deletegoodlist({ id: row.goods_id }).then((res) => {
                    console.log(81, res);
                    this.getgoodlist()
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        // 每页显示几条
        handleSizeChange(val) {
            this.pagesize = val
            this.getgoodlist()
        },
        // 当前第几页
        handleCurrentChange(val) {
            this.pagenum = val
            this.getgoodlist()
        },
        // 搜索
        searchgoodlist() {
            this.getgoodlist()
        },
        // 跳转添加页面
        goAddGoods() {
            this.$router.push('/addgoods')
        }
    },
    created() {
        this.getgoodlist()
    },
    // 时间过滤器
    filters: {
        filterTime(time) {
            return new Date(time * 1000).toLocaleString()
        }
    }
}
</script>
<style lang="scss" scoped></style>